package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.UserOderData;
import com.crecheng.shop.Manage.UserOderManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/userOder")
public class UserOderController {

    @RequestMapping("/get")
    public @ResponseBody
    String get(@RequestParam(value = "uid",defaultValue = "-1")int uid,
               @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultError;
        if(id<1)
            return JSON.toJSONString(UserOderManage.getByUser(uid));
        else
            return JSON.toJSONString(UserOderManage.get(id));
    }

    @RequestMapping("/del")
    public @ResponseBody
    String del(@RequestParam(value = "uid",defaultValue = "-1")int uid,
               @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1||id<1)
            return MyMessage.defaultError;

        if(UserOderManage.del(id))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    @RequestMapping("/add")
    public @ResponseBody
    String add(@RequestParam(value = "uid",defaultValue = "-1")int uid,
               @RequestParam(value = "gid",defaultValue = "-1")int gid,
               @RequestParam(value = "num",defaultValue = "-1")int num,
               @RequestParam(value = "aid",defaultValue = "-1")int aid,
               @RequestParam(value = "psMethod",defaultValue = "")String psMethod,
               @RequestParam(value = "pay",defaultValue = "")String pay){

        if(uid<1||gid<1||num<1||aid<1||psMethod.trim().length()<1||pay.trim().length()<1)
            return MyMessage.defaultError;
        UserOderData data=new UserOderData();
        data.set(uid,gid,num,aid,psMethod,pay);
        if(UserOderManage.add(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    
}
